package org.takeback.chat.store.pcegg;

import java.util.concurrent.*;
import java.io.*;
import org.takeback.chat.entity.*;
import org.joda.time.*;
import java.util.*;
import org.slf4j.*;

public class PcEggScheduler {
	private static final Logger LOGGER;
	private PcEggStore pcEggStore;

	public void init() throws IOException {
		
//		final Callable<Void> callable = (Callable<Void>) (() -> {
//			for (int i = 0; i < 5 && !this.pcEggStore.initData(); ++i) {
//				try {
//					TimeUnit.SECONDS.sleep(i * 2);
//				} catch (InterruptedException e) {
//					e.printStackTrace();
//				}
//			}
//			this.launchSchedule();
//			return null;
//		});
//		final FutureTask<Void> task = new FutureTask<Void>(callable);
//		new Thread(task).start();
	}

	private void launchSchedule() {
//		while (!Thread.currentThread().isInterrupted()) {
//			final PcEggLog pcEgg = this.pcEggStore.getLastest();
//			if (pcEgg != null && pcEgg.getExp() == null) {
//				this.schedule(pcEgg);
//				break;
//			}
//			PcEggScheduler.LOGGER.info("Cannot get new data, wait for 3 seconds to retry.");
//			try {
//				TimeUnit.SECONDS.sleep(3L);
//			} catch (InterruptedException e) {
//				e.printStackTrace();
//			}
//		}
	}

	private void schedule(final PcEggLog pcEgg) {
//		LocalDateTime localDateTime = LocalDateTime.fromDateFields(pcEgg.getExpireTime());
//		localDateTime = localDateTime.minusSeconds(10);
//		final Date workTime = localDateTime.toDate();
//		final Timer timer = new Timer(true);
//		timer.schedule(new TimerTask() {
//			@Override
//			public void run() {
//				if (PcEggScheduler.this.pcEggStore.work(pcEgg) && pcEgg.getLucky() != null) {
//					timer.cancel();
//					try {
//						TimeUnit.SECONDS.sleep(3L);
//					} catch (InterruptedException e) {
//						e.printStackTrace();
//					}
//					PcEggScheduler.this.launchSchedule();
//				} else {
//					final PeriodConfig config = PcEggScheduler.this.pcEggStore.getPeriodConfig(new LocalTime());
//					if (System.currentTimeMillis() - pcEgg.getExpireTime().getTime() > (config.getPeriodSeconds() - 30)
//							* 1000) {
//						timer.cancel();
//						try {
//							TimeUnit.SECONDS.sleep(20L);
//						} catch (InterruptedException e2) {
//							e2.printStackTrace();
//						}
//						PcEggScheduler.this.launchSchedule();
//					}
//				}
//			}
//		}, workTime, 5000L);
	}

	public PcEggStore getPcEggStore() {
		return this.pcEggStore;
	}

	public void setPcEggStore(final PcEggStore pcEggStore) {
		this.pcEggStore = pcEggStore;
	}

	static {
		LOGGER = LoggerFactory.getLogger((Class) PcEggScheduler.class);
	}
}
